package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTradeMarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
public class AdminBaseTradeMarkController {

    @Autowired
    private BaseTradeMarkService baseTradeMarkService;

    /**
     * 查询所有商标并且分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/baseTrademark/{pageNum}/{pageSize}")
    public Result<IPage<BaseTrademark>> getPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        IPage<BaseTrademark> IPage = new Page<>(pageNum, pageSize);
        IPage<BaseTrademark> baseTrademarkIPage = baseTradeMarkService.page(IPage, new QueryWrapper<BaseTrademark>().orderByAsc("id"));
        return Result.ok(baseTrademarkIPage);
    }

    /**
     * 保存商标
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result<Void> baseTrademarkSave(@RequestBody BaseTrademark baseTrademark) {
        baseTradeMarkService.save(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除商标
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result<Void> baseTrademarkRemove(@PathVariable("id") Long id) {
        baseTradeMarkService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据id获取商标
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result<BaseTrademark> getTrademarkById(@PathVariable("id") Long id) {
        return Result.ok(baseTradeMarkService.getById(id));
    }

    /**
     * 修改商标
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result<Void> baseTrademarkUpdate(@RequestBody BaseTrademark baseTrademark) {
        baseTradeMarkService.updateById(baseTrademark);
        return Result.ok();
    }

}
